import 'package:flutter/material.dart';

enum UiBadgeType { dot, normal }

class UiBadge extends StatelessWidget {
  const UiBadge({
    super.key,
    required this.child,
    this.content,
    this.position = const UiBadgePosition(right: -10, top: -10),
    this.show = false,
    this.color = Colors.red,
    this.padding = const EdgeInsets.symmetric(horizontal: 5),
    this.type = UiBadgeType.normal,
  });
  final Widget child;
  final Widget? content;
  final UiBadgePosition position;
  final bool show;
  final Color color;
  final EdgeInsetsGeometry padding;
  final UiBadgeType? type;
  bool get isDot {
    return content == null || type == UiBadgeType.dot;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      fit: StackFit.loose,
      clipBehavior: Clip.none,
      children: [
        child,
        Positioned(
          left: position.left,
          right: position.right,
          top: position.top,
          bottom: position.bottom,
          child: DefaultTextStyle(
            style: const TextStyle(color: Colors.white, fontSize: 12),
            child: AnimatedSwitcher(
              duration: kThemeAnimationDuration,
              child: show
                  ? Container(
                      padding: padding,
                      margin: EdgeInsets.only(
                        top: isDot ? 8 : 0,
                        right: isDot ? 5 : 0,
                      ),
                      decoration: BoxDecoration(
                        color: color,
                        borderRadius: BorderRadius.circular(9),
                      ),
                      constraints: BoxConstraints(
                        minWidth: isDot ? 10 : 18,
                        minHeight: isDot ? 10 : 18,
                      ),
                      alignment: Alignment.center,
                      child: type == UiBadgeType.dot ? null : content,
                    )
                  : Container(),
            ),
          ),
        ),
      ],
    );
  }
}

class UiBadgePosition {
  final double? top;
  final double? bottom;
  final double? right;
  final double? left;
  const UiBadgePosition({
    this.top,
    this.bottom,
    this.right,
    this.left,
  });
}
